package vin

import vin.Vin_base
import com.showapi.app.util.ConstantsOfShowapi
import com.showapi.app.util.CommonUtil
import org.apache.commons.lang3.StringUtils
import com.show.api.NormalRequest

//从数据库查
class Vin_chinacar extends Vin_base {
	@Override
	public Map doAction(reqParams,lineParams) {
		def ret=_getchinacar(reqParams.vin,lineParams)//用原版的来查就行
		return ret;
	}
	
	
	def _getchinacar={vin,lineParams->
		def req=new NormalRequest("http://www.chinacar.com.cn/vin_index.html")
		req.get()
		def vin_cookie,PHPSESSID
		req.getRes_headMap().find{
			def t=it.toString()
			def mat=t=~/(?is)vin_cookie=(\d+).*?PHPSESSID=(.*?);/
			mat.each{
				vin_cookie=it[1]
				PHPSESSID=it[2]
			}
			if(vin_cookie)return 1
		}
		def cookie="vin_cookie=${vin_cookie};PHPSESSID=${PHPSESSID};"
		
		def b=new NormalRequest("http://www.chinacar.com.cn/vin_index.html").setReadTimeout(10000)
		.addTextPara("leftvin", vin.substring(0,8))
		.addTextPara("rightvin", vin.substring(9))
		.addTextPara("textfield3", "")
		.addHeadPara("Cookie", cookie)
		.addTextPara("x", ((Math.random()*200) as int)+"")
		.addTextPara("y", ((Math.random()*200) as int)+"")
		.postAsByte()
		def str=new String(b)
//		println str
		int st=str.indexOf("class='table-list'")
		int end=str.indexOf('</table>',st)
		if(st==-1||end==-1)return
		str=str.substring(st,end)
		st=str.indexOf('href=')
		end=str.indexOf('>',st)
		if(st==-1||end==-1)return
		def url="http://www.chinacar.com.cn"+str.substring(st+5,end)
		println "aaaaaaaaaaaaaaaaaa"
		println url
		b=new NormalRequest(url).getAsByte()
		str=new String(b)
//		println str
		st=str.indexOf('生产企业信息')
		end=str.indexOf('</table>',st)
        if(st==-1||end==-1)return
		str=str.substring(st,end)
		def list=[]
		list<<[b:'车辆类型：.*?title="',e:'"',name:'car_type']
		list<<[b:'中文品牌：.*?<a.*?>',e:'<',name:'brand_name']
		list<<[b:'企业名称.*?<td.*?>',e:'<',name:'manufacturer']
		list<<[b:'外形尺寸.*?<span.*?>',e:'<',name:'car_body']
		list<<[b:'总质量.*?<span.*?>',e:'<',name:'car_weight']
		list<<[b:'额定载客.*?<td.*?>',e:'<',name:'seat_num']
		list<<[b:'排量.*?<tr.*?<td.*?<td.*?<td.*?>',e:'<',name:'output_volume']
		list<<[b:'<td.*?>',e:'<',name:'power']
		list<<[b:'燃料种类.*?<td.*?>',e:'<',name:'fuel_Type']
		def item=_getDefaultValue()
		CommonUtil.fillSingleMat(str,list,item)
		if(item.brand_name)item.brand_name= StringUtils.replace(item.brand_name, "牌", "")
		if(item.car_weight)item.car_weight= StringUtils.replace(item.car_weight, "(Kg)", "")
		if(item.seat_num)item.seat_num= StringUtils.replace(item.seat_num, "(人)", "")
		if(item.output_volume&&item.output_volume.isNumber()){
			item.output_volume= item.output_volume.toDouble()/1000 +""
		}
		
		item.from="chinacar"
        if(!ConstantsOfShowapi.IS_DEV){
			lineParams.needSave=true
        }
		return item
	}
	

	//在此可以从缓存中读取配置文件，获取权重数据
	@Override
	public int doSortWeight(reqParams,lineParams) {
		return 20;
	}

	

}
